package com.uaroadshow.finale.web.controller.api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.uaroadshow.entity.user.UserCourse;
import com.uaroadshow.finale.service.UserCourseService;

/**
 *
 * @author Ark
 *
 */
@RestController
@RequestMapping("/api/staffs")
public class StaffApiController {
    
    private static final Logger logger = LoggerFactory.getLogger(StaffApiController.class);

    @Autowired
    private UserCourseService userCourseService;
    
    /**
     * @param courseId
     * @param codes
     * @return
     */
    @RequestMapping(value = "/verificate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult verificate(@RequestParam("courseId") Long courseId, 
                                 @RequestParam("codes") List<String> codes) {
        
        HttpResult httpResult = new HttpResult();
        
        try {

            UserCourse userCourse = userCourseService.load(UserCourse.class, courseId);
            
            userCourseService.verificate(userCourse, codes);
            
            httpResult.success().message("核销成功");
            
        } catch (BusinessException e) {
            logger.error("#verificate() - verificate failed, business exception=[{}]", e);
            httpResult.fail("500").message(e.getMessage());
        } catch (Exception e) {
            logger.error("#verificate() - verificate failed, exception=[{}]", e);
            httpResult.fail("500").message("核销失败");
        }
        
        return httpResult;
        
    }
    
}
